rm(list = ls())
source("code/_preamble.R")

#________________________________
##Load the dataset
elections <- readRDS("data/elections.Rds")

#________________________________
##Main analysis----
#________________________________
#TABLE 1
dvs <- c("akp_per", "hdp_per", "chp_per", "mhp_per", "turnout")
main_res <- feols(.[dvs] ~ curfew + battle | neighborhood + election, cluster = ~ neighborhood, data = elections) 
control_res <- feols(.[dvs] ~ curfew + battle | neighborhood + election, cluster = ~ neighborhood, data = elections[elections$to_keep == 1, ]) 
duration_control_res <- feols(.[dvs] ~ curfew + battle | neighborhood + election, cluster = ~ neighborhood, data = elections[elections$to_keep_dur == 1, ]) 

#_____________________________
##printing the table
to_print <- rbind(
  round(c(main_res$`lhs: akp_per`$coefficients[["curfew"]], control_res$`lhs: akp_per`$coefficients[["curfew"]], duration_control_res$`lhs: akp_per`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: akp_per`$se[["curfew"]], control_res$`lhs: akp_per`$se[["curfew"]], duration_control_res$`lhs: akp_per`$se[["curfew"]]), 3), ")"),
  round(c(main_res$`lhs: hdp_per`$coefficients[["curfew"]], control_res$`lhs: hdp_per`$coefficients[["curfew"]], duration_control_res$`lhs: hdp_per`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: hdp_per`$se[["curfew"]], control_res$`lhs: hdp_per`$se[["curfew"]], duration_control_res$`lhs: hdp_per`$se[["curfew"]]), 3), ")"),
  round(c(main_res$`lhs: chp_per`$coefficients[["curfew"]], control_res$`lhs: chp_per`$coefficients[["curfew"]], duration_control_res$`lhs: chp_per`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: chp_per`$se[["curfew"]], control_res$`lhs: chp_per`$se[["curfew"]], duration_control_res$`lhs: chp_per`$se[["curfew"]]), 3), ")"),
  round(c(main_res$`lhs: mhp_per`$coefficients[["curfew"]], control_res$`lhs: mhp_per`$coefficients[["curfew"]], duration_control_res$`lhs: mhp_per`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: mhp_per`$se[["curfew"]], control_res$`lhs: mhp_per`$se[["curfew"]], duration_control_res$`lhs: mhp_per`$se[["curfew"]]), 3), ")"),
  round(c(main_res$`lhs: turnout`$coefficients[["curfew"]], control_res$`lhs: turnout`$coefficients[["curfew"]], duration_control_res$`lhs: turnout`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: turnout`$se[["curfew"]], control_res$`lhs: turnout`$se[["curfew"]], duration_control_res$`lhs: turnout`$se[["curfew"]]), 3), ")"),
  c(main_res$`lhs: akp_per`$nobs, control_res$`lhs: akp_per`$nobs, duration_control_res$`lhs: akp_per`$nobs)
)
print(xtable(to_print, digits = 3), include.rownames = F)
rm(list = setdiff(ls(), c("elections", "dvs")))


#__________________________________
##Appendix----
#__________________________________
#TABLE A1
elections$total_duration_days <- elections$total_duration/24

#_____________________________
##Analysis
main_res <- feols(.[dvs] ~ total_duration_days + battle | neighborhood + election, cluster = ~ neighborhood, data = elections) 
control_res <- feols(.[dvs] ~ total_duration_days + battle | neighborhood + election, cluster = ~ neighborhood, data = elections[elections$to_keep == 1, ]) 
duration_control_res <- feols(.[dvs] ~ total_duration_days + battle | neighborhood + election, cluster = ~ neighborhood, data = elections[elections$to_keep_dur == 1, ]) 

#_____________________________
##Printing the table
to_print <- rbind(
  round(c(main_res$`lhs: akp_per`$coefficients[["total_duration_days"]], control_res$`lhs: akp_per`$coefficients[["total_duration_days"]], duration_control_res$`lhs: akp_per`$coefficients[["total_duration_days"]]), 3),
  str_c("(", round(c(main_res$`lhs: akp_per`$se[["total_duration_days"]], control_res$`lhs: akp_per`$se[["total_duration_days"]], duration_control_res$`lhs: akp_per`$se[["total_duration_days"]]), 3), ")"),
  round(c(main_res$`lhs: hdp_per`$coefficients[["total_duration_days"]], control_res$`lhs: hdp_per`$coefficients[["total_duration_days"]], duration_control_res$`lhs: hdp_per`$coefficients[["total_duration_days"]]), 3),
  str_c("(", round(c(main_res$`lhs: hdp_per`$se[["total_duration_days"]], control_res$`lhs: hdp_per`$se[["total_duration_days"]], duration_control_res$`lhs: hdp_per`$se[["total_duration_days"]]), 3), ")"),
  round(c(main_res$`lhs: chp_per`$coefficients[["total_duration_days"]], control_res$`lhs: chp_per`$coefficients[["total_duration_days"]], duration_control_res$`lhs: chp_per`$coefficients[["total_duration_days"]]), 3),
  str_c("(", round(c(main_res$`lhs: chp_per`$se[["total_duration_days"]], control_res$`lhs: chp_per`$se[["total_duration_days"]], duration_control_res$`lhs: chp_per`$se[["total_duration_days"]]), 3), ")"),
  round(c(main_res$`lhs: mhp_per`$coefficients[["total_duration_days"]], control_res$`lhs: mhp_per`$coefficients[["total_duration_days"]], duration_control_res$`lhs: mhp_per`$coefficients[["total_duration_days"]]), 3),
  str_c("(", round(c(main_res$`lhs: mhp_per`$se[["total_duration_days"]], control_res$`lhs: mhp_per`$se[["total_duration_days"]], duration_control_res$`lhs: mhp_per`$se[["total_duration_days"]]), 3), ")"),
  round(c(main_res$`lhs: turnout`$coefficients[["total_duration_days"]], control_res$`lhs: turnout`$coefficients[["total_duration_days"]], duration_control_res$`lhs: turnout`$coefficients[["total_duration_days"]]), 3),
  str_c("(", round(c(main_res$`lhs: turnout`$se[["total_duration_days"]], control_res$`lhs: turnout`$se[["total_duration_days"]], duration_control_res$`lhs: turnout`$se[["total_duration_days"]]), 3), ")"),
  c(main_res$`lhs: akp_per`$nobs, control_res$`lhs: akp_per`$nobs, duration_control_res$`lhs: akp_per`$nobs)
)
print(xtable(to_print, digits = 3), include.rownames = F)
rm(list = setdiff(ls(), c("elections", "dvs")))


#__________________________________
#TABLE A5
main_res <- feols(.[dvs] ~ curfew + battle | neighborhood + election, cluster = ~ neighborhood, data = elections) 
control_res <- feols(.[dvs] ~ curfew + battle | neighborhood + election, cluster = ~ neighborhood, data = elections[elections$to_keep == 1, ]) 
duration_control_res <- feols(.[dvs] ~ curfew + battle | neighborhood + election, cluster = ~ neighborhood, data = elections[elections$to_keep_dur == 1, ]) 

#_____________________________
##Analysis
to_print <- rbind(
  round(c(main_res$`lhs: akp_per`$coefficients[["curfew"]], control_res$`lhs: akp_per`$coefficients[["curfew"]], duration_control_res$`lhs: akp_per`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: akp_per`$se[["curfew"]], control_res$`lhs: akp_per`$se[["curfew"]], duration_control_res$`lhs: akp_per`$se[["curfew"]]), 3), ")"),
  round(c(main_res$`lhs: akp_per`$coefficients[["battle"]], control_res$`lhs: akp_per`$coefficients[["battle"]], duration_control_res$`lhs: akp_per`$coefficients[["battle"]]), 3),
  str_c("(", round(c(main_res$`lhs: akp_per`$se[["battle"]], control_res$`lhs: akp_per`$se[["battle"]], duration_control_res$`lhs: akp_per`$se[["battle"]]), 3), ")"),
  round(c(main_res$`lhs: hdp_per`$coefficients[["curfew"]], control_res$`lhs: hdp_per`$coefficients[["curfew"]], duration_control_res$`lhs: hdp_per`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: hdp_per`$se[["curfew"]], control_res$`lhs: hdp_per`$se[["curfew"]], duration_control_res$`lhs: hdp_per`$se[["curfew"]]), 3), ")"),
  round(c(main_res$`lhs: hdp_per`$coefficients[["battle"]], control_res$`lhs: hdp_per`$coefficients[["battle"]], duration_control_res$`lhs: hdp_per`$coefficients[["battle"]]), 3),
  str_c("(", round(c(main_res$`lhs: hdp_per`$se[["battle"]], control_res$`lhs: hdp_per`$se[["battle"]], duration_control_res$`lhs: hdp_per`$se[["battle"]]), 3), ")"),
  round(c(main_res$`lhs: chp_per`$coefficients[["curfew"]], control_res$`lhs: chp_per`$coefficients[["curfew"]], duration_control_res$`lhs: chp_per`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: chp_per`$se[["curfew"]], control_res$`lhs: chp_per`$se[["curfew"]], duration_control_res$`lhs: chp_per`$se[["curfew"]]), 3), ")"),
  round(c(main_res$`lhs: chp_per`$coefficients[["battle"]], control_res$`lhs: chp_per`$coefficients[["battle"]], duration_control_res$`lhs: chp_per`$coefficients[["battle"]]), 3),
  str_c("(", round(c(main_res$`lhs: chp_per`$se[["battle"]], control_res$`lhs: chp_per`$se[["battle"]], duration_control_res$`lhs: chp_per`$se[["battle"]]), 3), ")"),
  round(c(main_res$`lhs: mhp_per`$coefficients[["curfew"]], control_res$`lhs: mhp_per`$coefficients[["curfew"]], duration_control_res$`lhs: mhp_per`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: mhp_per`$se[["curfew"]], control_res$`lhs: mhp_per`$se[["curfew"]], duration_control_res$`lhs: mhp_per`$se[["curfew"]]), 3), ")"),
  round(c(main_res$`lhs: mhp_per`$coefficients[["battle"]], control_res$`lhs: mhp_per`$coefficients[["battle"]], duration_control_res$`lhs: mhp_per`$coefficients[["battle"]]), 3),
  str_c("(", round(c(main_res$`lhs: mhp_per`$se[["battle"]], control_res$`lhs: mhp_per`$se[["battle"]], duration_control_res$`lhs: mhp_per`$se[["battle"]]), 3), ")"),
  round(c(main_res$`lhs: turnout`$coefficients[["curfew"]], control_res$`lhs: turnout`$coefficients[["curfew"]], duration_control_res$`lhs: turnout`$coefficients[["curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: turnout`$se[["curfew"]], control_res$`lhs: turnout`$se[["curfew"]], duration_control_res$`lhs: turnout`$se[["curfew"]]), 3), ")"),
  round(c(main_res$`lhs: turnout`$coefficients[["battle"]], control_res$`lhs: turnout`$coefficients[["battle"]], duration_control_res$`lhs: turnout`$coefficients[["battle"]]), 3),
  str_c("(", round(c(main_res$`lhs: turnout`$se[["battle"]], control_res$`lhs: turnout`$se[["battle"]], duration_control_res$`lhs: turnout`$se[["battle"]]), 3), ")"),
  c(main_res$`lhs: akp_per`$nobs, control_res$`lhs: akp_per`$nobs, duration_control_res$`lhs: akp_per`$nobs)
)
print(xtable(to_print, digits = 3), include.rownames = F)
rm(list = setdiff(ls(), c("elections", "dvs")))


#__________________________________
#TABLE A6
main_res <- feols(.[dvs] ~ curfew | neighborhood + election, cluster = ~ neighborhood, data = elections[elections$battle_unit == 1,]) 
etable(main_res, tex = T)
rm(list = setdiff(ls(), "elections"))



#___________________________________________
#TABLE A3

#___________________________________________
#get the sample
elections <- elections %>%
  group_by(town) %>%
  mutate(
    town_curfew = max(curfew), 
    town_to_keep = max(to_keep), 
    town_to_keep_dur = max(to_keep_dur)) %>%
  ungroup() %>%
  mutate(town_curfew = if_else(election == "november", town_curfew, 0),
         town_curfew = if_else(curfew == 1, 0, town_curfew),
         town_to_keep = if_else(to_keep == 1, 0, town_to_keep),
         town_to_keep_dur = if_else(to_keep_dur == 1, 0, town_to_keep_dur)
         )

#___________________________________________
#analysis
dvs <- c("akp_per", "hdp_per", "chp_per", "mhp_per", "turnout")
main_res <- feols(.[dvs] ~ town_curfew + battle | neighborhood + election, cluster = ~ neighborhood, data = elections) 
control_res <- feols(.[dvs] ~ town_curfew + battle | neighborhood + election, cluster = ~ neighborhood, data = elections[elections$town_to_keep == 1 & elections$to_keep == 0, ]) 
duration_control_res <- feols(.[dvs] ~ town_curfew + battle | neighborhood + election, cluster = ~ neighborhood, data = elections[elections$town_to_keep_dur == 1 & elections$to_keep_dur == 0, ]) 

#table
to_print <- rbind(
  round(c(main_res$`lhs: akp_per`$coefficients[["town_curfew"]], control_res$`lhs: akp_per`$coefficients[["town_curfew"]], duration_control_res$`lhs: akp_per`$coefficients[["town_curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: akp_per`$se[["town_curfew"]], control_res$`lhs: akp_per`$se[["town_curfew"]], duration_control_res$`lhs: akp_per`$se[["town_curfew"]]), 3), ")"),
  round(c(main_res$`lhs: hdp_per`$coefficients[["town_curfew"]], control_res$`lhs: hdp_per`$coefficients[["town_curfew"]], duration_control_res$`lhs: hdp_per`$coefficients[["town_curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: hdp_per`$se[["town_curfew"]], control_res$`lhs: hdp_per`$se[["town_curfew"]], duration_control_res$`lhs: hdp_per`$se[["town_curfew"]]), 3), ")"),
  round(c(main_res$`lhs: chp_per`$coefficients[["town_curfew"]], control_res$`lhs: chp_per`$coefficients[["town_curfew"]], duration_control_res$`lhs: chp_per`$coefficients[["town_curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: chp_per`$se[["town_curfew"]], control_res$`lhs: chp_per`$se[["town_curfew"]], duration_control_res$`lhs: chp_per`$se[["town_curfew"]]), 3), ")"),
  round(c(main_res$`lhs: mhp_per`$coefficients[["town_curfew"]], control_res$`lhs: mhp_per`$coefficients[["town_curfew"]], duration_control_res$`lhs: mhp_per`$coefficients[["town_curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: mhp_per`$se[["town_curfew"]], control_res$`lhs: mhp_per`$se[["town_curfew"]], duration_control_res$`lhs: mhp_per`$se[["town_curfew"]]), 3), ")"),
  round(c(main_res$`lhs: turnout`$coefficients[["town_curfew"]], control_res$`lhs: turnout`$coefficients[["town_curfew"]], duration_control_res$`lhs: turnout`$coefficients[["town_curfew"]]), 3),
  str_c("(", round(c(main_res$`lhs: turnout`$se[["town_curfew"]], control_res$`lhs: turnout`$se[["town_curfew"]], duration_control_res$`lhs: turnout`$se[["town_curfew"]]), 3), ")"),
  c(main_res$`lhs: akp_per`$nobs, control_res$`lhs: akp_per`$nobs, duration_control_res$`lhs: akp_per`$nobs)
)
print(xtable(to_print, digits = 3), include.rownames = F)


